package com.jz.kufangkuqu.shuxing.controller;


import com.jz.kufangkuqu.shuxing.bean.ShuXingAdd;
import com.jz.kufangkuqu.shuxing.bean.ShuXingSelect;
import com.jz.kufangkuqu.shuxing.bean.ShuXingUpdate;
import com.jz.kufangkuqu.shuxing.bean.ShuXingUpdatestatus;
import com.jz.kufangkuqu.shuxing.service.ShuXingService;
import com.jz.util.ResultBuildVO;
import com.jz.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;


@RestController
@CrossOrigin
public class ShuXingController {

    @Autowired
    private ShuXingService shuXingService;

    //分页
    @PostMapping("pickingArea/queryPage")
    public ResultVO querypage(@RequestBody ShuXingSelect shuXingSelectVo){
        if(shuXingSelectVo.getPageIndex()==null){
            shuXingSelectVo.setPageIndex(1);
        }
        if(shuXingSelectVo.getPageSize()==null){
            shuXingSelectVo.setPageSize(10);
        }
        ResultVO resultVO = shuXingService.queryPage(shuXingSelectVo);
        return resultVO;
    }

    //新增
    @PostMapping("pickingArea/add")
    public ResultVO add(@RequestBody @Valid ShuXingAdd shuXingAdd, BindingResult bindingResult ){
        if (bindingResult.hasErrors()){
            return ResultBuildVO.error(bindingResult.getFieldError().getDefaultMessage());
        }
        ResultVO resultVO = shuXingService.add(shuXingAdd);
        return resultVO;
    }

    //修改
    @PostMapping("pickingArea/update")
    public ResultVO update(@RequestBody @Valid ShuXingUpdate shuXingUpdate, BindingResult bindingResult ){
        if (bindingResult.hasErrors()){
            return ResultBuildVO.error(bindingResult.getFieldError().getDefaultMessage());
        }
        ResultVO resultVO = shuXingService.update(shuXingUpdate);
        return resultVO;
    }
    //启用禁用
    @PostMapping("pickingArea/updateStatus")
    public ResultVO updateStatus(@RequestBody @Valid ShuXingUpdatestatus shuXingUpdatestatus, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuildVO.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return shuXingService.updateStatus(shuXingUpdatestatus);
    }
}